/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 04/03/2012, 15:46:26
 */

package br.edu.fatec.ads.view;

import br.edu.fatec.ads.classes.Funcionario;
import br.edu.fatec.ads.dao.HibernateGenericDAO;
import br.edu.fatec.ads.facade.FacadePessoaComplemento;
import br.edu.fatec.ads.util.FrameUtil;
import br.edu.fatec.ads.util.SessionUtil;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;

/**
 *
 * @author Ubirajara
 */
public class Login extends javax.swing.JFrame {

    private HibernateGenericDAO dao = null;

    /** Creates new form Login */
    public Login() {
        initComponents();

        FrameUtil.getInstance().centralizarFrame(this);
    }

    public Login(HibernateGenericDAO dao){
        this();
        this.dao = dao;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        jPanel1 = new javax.swing.JPanel();
        btnEntrar = new javax.swing.JButton();
        txtLogin = new javax.swing.JTextField();
        txtSenha = new javax.swing.JPasswordField();
        lblLogin = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        lblLogoFatec = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Faculdade de Tecnologia da Baixada Santista");

        jPanel1.setBackground(new java.awt.Color(233, 251, 232));

        btnEntrar.setText("Entrar");
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });

        txtLogin.setColumns(12);
        txtLogin.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));

        txtSenha.setColumns(12);
        txtSenha.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.black, null));

        lblLogin.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblLogin.setText("Login:");

        lblSenha.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblSenha.setText("Senha");

        lblLogoFatec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/fatec/ads/image/logo.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblLogin)
                    .addComponent(lblSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .addComponent(btnEntrar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                    .addComponent(lblLogoFatec)
                    .addComponent(txtLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
                .addGap(107, 107, 107))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblLogoFatec)
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLogin)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSenha)
                    .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEntrar)
                .addGap(75, 75, 75))
        );

        jPanel1.setBounds(0, 0, 400, 260);
        desktopPane.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        menuBar.setBackground(new java.awt.Color(241, 244, 230));
        menuBar.setPreferredSize(new java.awt.Dimension(0, 20));
        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        Funcionario usuario = null;

        // Validacao no BD
        String login = txtLogin.getText();
        String senha = String.valueOf(txtSenha.getPassword());
        
        try{
            usuario = FacadePessoaComplemento.getInstance().login(login, senha);

            if(usuario != null){
                SessionUtil.getInstance().setParametro("usuario", usuario);

                FrameUtil.getInstance().adicionarMensagemSucesso("Bem vindo, " + usuario.getPessoa().getNome());

                //Deixa visivel o frame do Menu Principal
                TelaPrincipal principal = new TelaPrincipal();
                principal.setVisible(true);

                //Efetua o fechamento do frame de login (acesso)
                this.dispose();
            }else{
                JOptionPane.showMessageDialog(null, "Erro ao buscar pessoa");
            }
        }catch(HibernateException cause){
            FrameUtil.getInstance().adicionarMensagemErro(cause.getMessage());
        }
    }//GEN-LAST:event_btnEntrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblLogoFatec;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JPasswordField txtSenha;
    // End of variables declaration//GEN-END:variables

}
